/**
 * author      : dormi330
 * date        : 2018/6/27
 * project     : mybatis
 * description : TODO
 */

package com.deep.trade.infrastructure.mybatis.order;

import com.deep.trade.domain.order.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class MybatisOrderQueryRepository implements OrderQueryRepository {

    @Autowired
    private TradeOrderMapper tradeOrderMapper;

    @Autowired
    private OrderSummaryMapper orderSummaryMapper;

    @Autowired
    private DeliveryAddressMapper deliveryAddressMapper;

    @Autowired
    private TradeOrderSkuMapper tradeOrderSkuMapper;

    @Autowired
    private OrderPaymentMapper orderPaymentMapper;

    /**
     * 查询单个 OrderSummary
     */
    public OrderSummary orderSummaryOfId(String tenantCode, String orderSummaryId) {
        return orderSummaryMapper.queryById(tenantCode, orderSummaryId);
    }

    public PageInfo<OrderSummary> orderSummaryOfMember(String tenantCode, String memberId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<OrderSummary> tradeOrderList = orderSummaryMapper.queryByMember(tenantCode, memberId);
        return new PageInfo<>(tradeOrderList);
    }

    /**
     * 查询 OrderSummary 的所有 TradeOrder
     */
    public List<TradeOrder> tradeOderOfOrderSummary(String tenantCode, String orderSummaryId) {
        return tradeOrderMapper.queryByOrderSummary(tenantCode, orderSummaryId);
    }


    /**
     * 查询单个 TradeOrder
     */
    public TradeOrder tradeOrderOfId(String tenantCode, String tradeOrderId) {
        return tradeOrderMapper.queryById(tenantCode, tradeOrderId);
    }

    /**
     * 分页查询, 会员的所有订单
     */
    public PageInfo<TradeOrder> tradeOrderOfMember(String tenantCode, String memberId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        // Mybatis查询方法
        List<TradeOrder> tradeOrderList = tradeOrderMapper.queryByMember(tenantCode, memberId);
        // 用PageInfo对结果进行包装
        PageInfo<TradeOrder> pageInfo = new PageInfo(tradeOrderList);
        return pageInfo;
    }

    /**
     * 分页查询, 租户的所有订单
     */
    public PageInfo<TradeOrder> tradeOrderOfTenant(String tenantCode, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TradeOrder> tradeOrderList = tradeOrderMapper.queryByTenant(tenantCode);
        return new PageInfo<>(tradeOrderList);
    }

    /**
     * 根据 租户 + tradeOrder , 查询订单的所属sku 列表
     */
    public List<TradeOrderSku> orderSkuOfTradeOrder(String tenantCode, String tradeOrderId) {
        return tradeOrderSkuMapper.queryListByTradeOrder(tenantCode, tradeOrderId);
    }

    public List<TradeOrderSku> orderSkuOfTradeOrderList(String tenantCode, List<String> tradeOrderIdList) {
        return tradeOrderSkuMapper.queryListByTradeOrderList(tenantCode, tradeOrderIdList);
    }


    /**
     * 根据 租户 + tradeOrder , 查询 TradeOrder 的 收货地址信息
     */
    public DeliveryAddress orderDeliveryOfTradeOrder(String tenantCode, String tradeOrderId) {
        return deliveryAddressMapper.queryByTradeOrder(tenantCode, tradeOrderId);
    }

    /**
     * 根据 租户 + tradeOrder , 查询 TradeOrder 的 的支付信息
     */
    public OrderPayment paidInoOfTradeOrder(String tenantCode, String tradeOrderId) {
        return orderPaymentMapper.queryByTradeOrder(tenantCode, tradeOrderId);
    }

}
